
import re

from rest_framework import serializers
from rest_framework.validators import ValidationError

from .models import *

# 用户序列化器
class UserSer(serializers.ModelSerializer):
    clbum = serializers.PrimaryKeyRelatedField(queryset=ClbumModel.objects.all())
    role = serializers.PrimaryKeyRelatedField(queryset=RoleModel.objects.all())

    clbum_name = serializers.SerializerMethodField()
    role_name = serializers.SerializerMethodField()
    def get_clbum_name(self, obj):
        return obj.clbum.name
    def get_role_name(self, obj):
        return obj.role.name

    def validate(self, attrs):
        account = attrs.get("account")
        password = attrs.get("password")

        if not re.findall(r"^[\u4e00-\u9fa5][\u4e00-\u9fa5\w]{1,9}$", account):
            raise ValidationError(f'{account} 姓名由2到10位汉字、数字、字母或下划线组成！')

        if not re.findall(r"^[\w\W]{6,256}$", password):
            raise ValidationError(f'密码由6到20位数字、字母或下划线组成！')

        return attrs

    class Meta:
        model = UserModel
        fields = '__all__'
        read_only_fields = ["id"]

# 用户信息序列化器
class UserInfoSer(serializers.ModelSerializer):
    sex = serializers.ChoiceField(choices=UserinfoModel.TYPE, source="get_sex_display", read_only=True)

    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())

    user_account = serializers.SerializerMethodField()
    def get_user_account(self,obj):
        return obj.user.account

    def validate(self, attrs):

        mobile = attrs.get('mobile')
        name = attrs.get('name')

        if not re.findall(r"^1[3-9]\d{9}$", f'{mobile}'):
            raise ValidationError(f'{mobile} 手机号格式不正确！')

        if not name:
            return ValidationError('姓名不能为空')

        return attrs

    class Meta:
        model = UserinfoModel
        fields = '__all__'
        read_only_fields = ["id"]

# 学校序列化器
class SchoolSer(serializers.ModelSerializer):

    class Meta:
        model = SchoolModel
        fields = '__all__'
        read_only_fields = ["id"]

# 班级序列化器
class ClbumSer(serializers.ModelSerializer):

    school = serializers.PrimaryKeyRelatedField(queryset=SchoolModel.objects.all())

    school_name = serializers.SerializerMethodField()
    def get_school_name(self, obj):
        return obj.school.name

    class Meta:
        model = ClbumModel
        fields = '__all__'
        read_only_fields = ["id","school"]

# 问候语序列化器
class GreetingSer(serializers.ModelSerializer):

    class Meta:
        model = GreetingsModel
        fields = '__all__'
        read_only_fields = ["id"]

# 节日问候语序列化器
class FesGreetingSer(serializers.ModelSerializer):
    date_type = serializers.ChoiceField(choices=FesGreetingsModel.TYPE, source="get_date_type_display", read_only=True)

    class Meta:
        model = FesGreetingsModel
        fields = '__all__'
        read_only_fields = ["id"]

# 消息序列化器
class MessageSer(serializers.ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    users = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())

    user_account = serializers.SerializerMethodField()
    users_account = serializers.SerializerMethodField()
    def get_user_account(self, obj):
        return obj.user.account
    def get_users_account(self, obj):
        return obj.users.account

    class Meta:
        model = MessageModel
        fields = '__all__'
        read_only_fields = ["id"]

# 菜单序列化器
class MenuSer(serializers.ModelSerializer):

    class Meta:
        model = MenuModel
        fields = '__all__'
        read_only_fields = ['id']

# 角色菜单序列化器
class RoleMenuSer(serializers.ModelSerializer):
    role = serializers.PrimaryKeyRelatedField(queryset=RoleModel.objects.all())
    menu = serializers.PrimaryKeyRelatedField(queryset=MenuModel.objects.all())

    role_name = serializers.SerializerMethodField()
    menu_name = serializers.SerializerMethodField()

    def get_role_name(self, obj):
        return obj.role.name
    def get_menu_name(self, obj):
        return obj.menu.name

    class Meta:
        model = RoleMenuModel
        fields = '__all__'
        read_only_fields = ['id']

